﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace GestureListenerClient.Services.HTTP
{
    public class HTTPClient
    {
        /// <summary>
        /// Perform a HTTP get request to a specific URL
        /// </summary>
        /// <param name="path">URL to request from</param>
        /// <returns>Response text</returns>
        /// <exception cref="System.Net.WebException"></exception>
        public static string get(Uri path)
        {
            //Resolve the addres to a hostname. Proxy didn't like all IP's
            IPHostEntry entry = System.Net.Dns.GetHostEntry(path.DnsSafeHost);
            Uri hostUri = new Uri(path.Scheme + "://" + entry.HostName + ":" + path.Port + path.PathAndQuery);

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(hostUri);
            request.Method = "GET";

            //Just get plain text back, and spit it back
            request.Accept = "text/plain";

            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream respStream = response.GetResponseStream();

                StreamReader reader = new StreamReader(new BufferedStream(response.GetResponseStream()));
                string data = reader.ReadToEnd();
                return data;
            }
            catch (WebException webEx)
            {
                //Perhaps handle later?
                throw webEx;
            }
        }

        /// <summary>
        /// Perform a HTTP POST command on a specific URL
        /// </summary>
        /// <param name="path">URL to set data at</param>
        /// <param name="data">Data to set</param>
        /// <returns>Response stream text</returns>
        /// <exception cref="System.Net.WebException"></exception>
        public static string post(Uri path, string data)
        {
            //Resolve the addres to a hostname. Proxy didn't like all IP's
            IPHostEntry entry = System.Net.Dns.GetHostEntry(path.DnsSafeHost);
            Uri hostUri = new Uri(path.Scheme + "://" + entry.HostName + ":" + path.Port + path.PathAndQuery);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);
            request.Method = "POST";

            //Encode the data
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] dataArr = Encoding.ASCII.GetBytes(data);
            request.ContentLength = dataArr.Length;
            request.ContentType = "text/plain";

            //Write the data to the request's stream
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(dataArr, 0, dataArr.Length);
            dataStream.Close();

            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream respStream = response.GetResponseStream();

                StreamReader reader = new StreamReader(new BufferedStream(response.GetResponseStream()));
                string respData = reader.ReadToEnd();

                dataStream.Close();
                return respData;
            }
            catch (WebException webEx)
            {
                //Perhaps handle later?
                throw webEx;
            }
        }
    }
}
